<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
    <style>
        .barrage_content{
            width: 100%;
            height: 800px;
            background-color: #d5d5d5;
            position: relative;
            overflow: hidden;
        }
        .barrage_input{
            width: 200px;
            height: 32px;
        }
        .barrage{
            background-color: #00aaff;
            line-height: 2rem;
            padding: 0.3rem 1rem;
            border-radius: 0.5rem;
            color: white;
            left:100%;
            position: absolute;
            animation: move 10s;
            animation-timing-function: linear;
            animation-iteration-count: 1;
            animation-fill-mode :forwards;
            overflow: hidden;
            height: 2rem;
            display: block;
        }
        @keyframes move {
            from{left:100%;}
            to{left: -20%}
        }
    </style>
</head>
<body>
<div class="barrage_content">
    <span class="barrage">这是一条弹幕</span>
</div>
<input class="barrage_input" type="text" name="" id="">
<button onclick="send_barrage()">发送</button>
</body>
<script>
    var content=document.getElementsByClassName("barrage_content")[0];
    var conHeight=content.offsetHeight;

    //alert(conHeight);
    function send_barrage() {
        //使用随机数判断弹幕位置
        var h=(Math.random()*conHeight)-50;
        if(h<100){
            h=100;
        }
        var con=document.getElementsByClassName("barrage_input")[0].value;
        var bar=document.createElement("span");
        bar.setAttribute("class","barrage");
        bar.innerHTML=con;
        bar.style.top=h+"px";
        content.appendChild(bar);
    }
    setInterval(remov,2000);
    function remov() {
        var bars=document.getElementsByClassName("barrage");
        for(var i=0;i<bars.length;i++){
            if(bars[i].offsetLeft<-100){
                bars[i].parentNode.removeChild(bars[i]);
            }
        }
    }
</script>
</html>